﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Nivlag
{
    public class SharePointTree
    {
        public string someProperty;

        public SharePointTree( string siteUrl) {
            someProperty = siteUrl;
        }


    } // SharePointTree class

    public class RecursiveTraversOR
    {
        string myStartingPoint;

        public string MyStartingPoint
        {
            get { return myStartingPoint; }
            set { myStartingPoint = value; }
        }

        /// <summary>
        /// Starts at the startingPoint and finds all the stuff it can find.
        /// </summary>
        /// <param name="startingPoint"></param>
        public RecursiveTraversOR(string startingPoint)
        {
            myStartingPoint = startingPoint;

        } // RecursiveTraversOR


        public string Traverse(string rootUrl, string xmlTree)
        {
            SPSite siteColl;
            siteColl = new SPSite(rootUrl);

            SPWeb rootWeb = siteColl.RootWeb;

            try
            {
                xmlTree = 
                    "<sitecollection url='" + rootWeb.Url + "'>";

                // Loop thru all the sites in this site collection
                foreach (SPWeb aWeb in rootWeb.Webs)
                {

                    xmlTree += TraverseSubWeb(aWeb, xmlTree);

                } // iterating thru SPWebs

                
                xmlTree += "</sitecollection>";

                return xmlTree;
                
            } // trying out the root site.

            catch (System.IO.FileNotFoundException fne)
            {

                return "Could not find that site.";

            }
        } // Traverse()

        public string TraverseSubWeb(SPWeb theWeb, string xmlTree)
        {
            try
            {

                SPSite sitecoll = theWeb.Site;

                SPWebTemplate spwt;

                string webTemplateName = null;

                try
                {
                    // Get the English version of the template (1033 = english lang code)
                    spwt = sitecoll.GetWebTemplates(1033)[theWeb.WebTemplateId];

                    webTemplateName = spwt.Description;

                }
                catch (Exception e)
                {
                    webTemplateName = "[could not resolve]";
                }

                xmlTree = "<web title='" + theWeb.Title + "'  " +
                  "Template='" + webTemplateName + "'>";

                // Loop thru all the sites in the site collection
                foreach (SPList aList in theWeb.Lists)
                {

                    string templateName;

                    try
                    {
                        templateName = theWeb.Features[aList.TemplateFeatureId].Definition.DisplayName;
                    }
                    catch (Exception e)
                    {
                        templateName = "[Cannot resolve]";
                    }

                    if (templateName == null)
                        templateName = "[null]";

                    xmlTree += 
                        "<list title='" + aList.Title + "' " +
                        "Template='" + templateName + "' " +
                        "listid='" + theWeb.Url + CONSTANTS.UrlWebDelimiter + aList.ID + "'" +
                        "></list>";

                    ;

                } // foreach

                // Loop thru all the sites in this site collection
                foreach (SPWeb aWeb in theWeb.Webs)
                {

                    xmlTree += TraverseSubWeb(aWeb, xmlTree);

                } // iterating thru SPWebs

                xmlTree += "</web>";

                return xmlTree;

            } // trying out the root site.

            catch (System.IO.FileNotFoundException fne)
            {

                return "Could not find that site.";

            }
        } // TraverseSubWeb()



    }
}
